package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.BdMoneyDrawDao;
import com.fdb.efp.nls.service.domain.BdMoneyDraw;
import com.fdb.efp.nls.service.facade.BdMoneyDrawService;
import com.fdb.efp.nls.service.facade.BdMoneyDrawTempService;
import com.fdb.efp.nls.service.vo.BdMoneyDrawTempVO;
import com.fdb.efp.nls.service.vo.BdMoneyDrawVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 托管机构资金划扣明细表
 * <p>
 * 
 * @author ligm
 * @date 2019-03-07
 */
@Service("bdMoneyDrawService")
public class BdMoneyDrawServiceImpl implements BdMoneyDrawService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BdMoneyDrawServiceImpl.class);

	@Autowired
	private BdMoneyDrawDao bdMoneyDrawDao;

	@Autowired
	@Qualifier("bdMoneyDrawTempService")
	private BdMoneyDrawTempService bdMoneyDrawTempService;

	/**
	 * 新增操作
	 */
	public int insertBdMoneyDraw(BdMoneyDrawVO inBdMoneyDrawVo) {
		logger.debug("当前新增数据为:" + inBdMoneyDrawVo.toString());
		int num = 0;
		try {
			BdMoneyDraw bdMoneyDraw = new BdMoneyDraw();
			beanCopy(inBdMoneyDrawVo, bdMoneyDraw);
			num = bdMoneyDrawDao.insertBdMoneyDraw(bdMoneyDraw);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BdMoneyDrawVO inBdMoneyDrawVo) {
		logger.debug("当前删除数据条件为:" + inBdMoneyDrawVo);
		int num = 0;
		try {
			BdMoneyDraw bdMoneyDraw = new BdMoneyDraw();
			beanCopy(inBdMoneyDrawVo, bdMoneyDraw);
			num = bdMoneyDrawDao.deleteByPk(bdMoneyDraw);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdMoneyDrawVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(BdMoneyDrawVO inBdMoneyDrawVo) {
		logger.debug("当前修改数据为:" + inBdMoneyDrawVo.toString());
		int num = 0;
		try {
			BdMoneyDraw bdMoneyDraw = new BdMoneyDraw();
			beanCopy(inBdMoneyDrawVo, bdMoneyDraw);
			num = bdMoneyDrawDao.updateByPk(bdMoneyDraw);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdMoneyDrawVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BdMoneyDrawVO queryByPk(BdMoneyDrawVO inBdMoneyDrawVo) {

		logger.debug("当前查询参数信息为:" + inBdMoneyDrawVo);
		try {
			BdMoneyDraw queryBdMoneyDraw = new BdMoneyDraw();
			beanCopy(inBdMoneyDrawVo, queryBdMoneyDraw);
			BdMoneyDraw queryRslBdMoneyDraw = bdMoneyDrawDao.queryByPk(queryBdMoneyDraw);
			if (Objects.nonNull(queryRslBdMoneyDraw)) {
				BdMoneyDrawVO outBdMoneyDrawVo = beanCopy(queryRslBdMoneyDraw, new BdMoneyDrawVO());
				logger.debug("当前查询结果为:" + outBdMoneyDrawVo.toString());
				return outBdMoneyDrawVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdMoneyDrawVO> queryAllOwner(BdMoneyDrawVO bdMoneyDrawVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BdMoneyDrawVO> list = null;
		try {
			List<BdMoneyDraw> bdMoneyDraws = bdMoneyDrawDao.queryAllOwnerByPage(bdMoneyDrawVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + bdMoneyDraws.size());
			pageSet(bdMoneyDraws, bdMoneyDrawVo);
			list = (List<BdMoneyDrawVO>) beansCopy(bdMoneyDraws, BdMoneyDrawVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdMoneyDrawVO> queryAllCurrOrg(BdMoneyDrawVO bdMoneyDrawVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BdMoneyDraw> bdMoneyDraws = bdMoneyDrawDao.queryAllCurrOrgByPage(bdMoneyDrawVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + bdMoneyDraws.size());
		List<BdMoneyDrawVO> list = null;
		try {
			pageSet(bdMoneyDraws, bdMoneyDrawVo);
			list = (List<BdMoneyDrawVO>) beansCopy(bdMoneyDraws, BdMoneyDrawVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdMoneyDrawVO> queryAllCurrDownOrg(BdMoneyDrawVO bdMoneyDrawVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BdMoneyDraw> bdMoneyDraws = bdMoneyDrawDao.queryAllCurrDownOrgByPage(bdMoneyDrawVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bdMoneyDraws.size());
		List<BdMoneyDrawVO> list = null;
		try {
			pageSet(bdMoneyDraws, bdMoneyDrawVo);
			list = (List<BdMoneyDrawVO>) beansCopy(bdMoneyDraws, BdMoneyDrawVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 
	 * 批量新增百度托管机构资金划扣明细信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchInsertBdMoneyDraw(List<BdMoneyDrawTempVO> addList) {
		logger.debug("当前新增数据条数为:" + addList.size());
		List<BdMoneyDraw> bdMoneyDrawList = new ArrayList<BdMoneyDraw>();
		int num = 0;
		try {
			num = bdMoneyDrawDao.batchInsertBdMoneyDraw(bdMoneyDrawList);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 
	 * 批量更新百度托管机构资金划扣明细信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchUpdateBdMoneyDraw(List<BdMoneyDrawTempVO> updateList) {
		logger.debug("当前修改数据为:" + updateList.size() + "条");
		List<BdMoneyDraw> bdMoneyDrawList = new ArrayList<BdMoneyDraw>();
		int num = 0;
		try {
			num = bdMoneyDrawDao.batchUpdateBdMoneyDraw(bdMoneyDrawList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	/**
	 * 
	 * 同步托管机构资金划扣临时表数据到托管机构资金划扣正式表有数据则更新，无则新增 <br>
	 * 0.1:ludayong:2019年3月23日 下午12:09:28:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月23日 下午12:09:28
	 * @date 2019年3月23日 下午12:09:28
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateBdMoneyDraw(List<BdMoneyDrawTempVO> list) {
		LocalDate today = LocalDate.now();
		String msg1 = "【托管机构资金划扣明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<BdMoneyDraw> bdMoneyDrawList = new ArrayList<BdMoneyDraw>();
		int num = 0;
		try {
			bdMoneyDrawList = (List<BdMoneyDraw>) beansCopy(list, BdMoneyDraw.class);
			num = bdMoneyDrawDao.insertOrUpdateBdMoneyDraw(bdMoneyDrawList);
			String num_msg = "【托管机构资金划扣明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【托管机构资金划扣明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
}
